class TestBuoyancy extends Test
{
    private m_bodies:Box2D.Dynamics.b2Body[] = [];
    private m_controller:Box2D.Dynamics.Controllers.b2Controller;

    public constructor(sprite:DebugDraw)
    {
        super(sprite);
        this.createScene();
    }

    private createScene():void
    {
        let bc:Box2D.Dynamics.Controllers.b2BuoyancyController = new Box2D.Dynamics.Controllers.b2BuoyancyController();
        this.m_controller = bc;

        bc.normal.Set(0,-1);
        bc.offset = -200 / this.m_physScale;
        bc.density = 2.0;
        bc.linearDrag = 5;
        bc.angularDrag = 2;

        let ground:Box2D.Dynamics.b2Body = this.m_world.GetGroundBody();
        let i:number;
        let len:number;
        let anchor:Box2D.Common.Math.b2Vec2 = new Box2D.Common.Math.b2Vec2();
        let body:Box2D.Dynamics.b2Body;
        let fd:Box2D.Dynamics.b2FixtureDef;

        //spawn in a bunch of crap

        for(i =0; i < 5; i++)
        {
            let bodyDef:Box2D.Dynamics.b2BodyDef = new Box2D.Dynamics.b2BodyDef();
            bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
            // bodyDef.bullet = true;
            let boxDef:Box2D.Collision.Shapes.b2PolygonShape = new Box2D.Collision.Shapes.b2PolygonShape();
            fd = new Box2D.Dynamics.b2FixtureDef();
            fd.shape = boxDef;
            fd.density = 1.0;
            //override the default frictrion
            fd.friction = 0.3;
            fd.restitution = 0.1;
            boxDef.SetAsBox((Math.random() * 5 + 10) / this.m_physScale, (Math.random() * 5 + 10) / this.m_physScale);
            bodyDef.position.Set((Math.random() * 400 + 120) / this.m_physScale,(Math.random() * 150 + 50) / this.m_physScale);
            bodyDef.angle = Math.random() * Math.PI;
            body = this.m_world.CreateBody(bodyDef);
            body.CreateFixture(fd);
            this.m_bodies.push(body);
            
        }

        for(i = 0, len = this.m_bodies.length; i < len; i++)
        {
            this.m_controller.AddBody(this.m_bodies[i]);
        }
        this.m_world.AddController(this.m_controller);
    }

    public update():void
    {
        super.update();
        //draw water line
        this.m_sprite.graphics.lineStyle(1,0x0000ff,1);
        this.m_sprite.graphics.moveTo(5, 200);
        this.m_sprite.graphics.lineTo(635, 200);
        //it's not water without transparency;
        this.m_sprite.graphics.lineStyle();
        this.m_sprite.graphics.beginFill(0x0000ff, 0.2);
        this.m_sprite.graphics.moveTo(5,200);
        this.m_sprite.graphics.lineTo(635,200);
        this.m_sprite.graphics.lineTo(635,355);
        this.m_sprite.graphics.lineTo(5,355);
        this.m_sprite.graphics.endFill();
    }
}